<template>
    <div>
        <div class="van-uploader">
            <div class="cell head">
                <div>头像</div>
                <img :src="userInfo.avatar" class="pic">
            </div>
            <input type="file" class="file" @change="changeAvatar">
        </div>
        <div class="line"></div>
        <div class="cell">
            <div>昵称</div>
            <div class="name">
                <input v-model="userInfo.nickname"/>
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell" @click="isShow=true">
            <div>性别</div>
            <div class="name">
                <div>{{gender}}</div>
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell">
            <div>年龄</div>
            <div class="name">
                <input v-model="userInfo.age" @focus="focus"/>
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <!--<div class="line"></div>-->
        <!--<div class="cell">-->
        <!--<div>微信号</div>-->
        <!--<div class="name">-->
        <!--<input v-model="userInfo.name"/>-->
        <!--<svg aria-hidden="true" class="right">-->
        <!--<use xlink:href="#icon-chevronright"></use>-->
        <!--</svg>-->
        <!--</div>-->
        <!--</div>-->
        <div class="line"></div>
        <div class="cell">
            <div>手机号</div>
            <div class="name">
                <input v-model="userInfo.infophone" @keyup="mett($event)"/>
                <!--                <p>{{userInfo.infophone}}</p>-->
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell">
            <div>同步手机号</div>
            <div class="name">
                <p>{{userInfo.sphone}}</p>
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell">
            <div>所在省市区</div>
            <div class="name">
                <div>{{areaData}}</div>
                <svg aria-hidden="true" class="right">
                    <use xlink:href="#icon-chevronright"></use>
                </svg>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell" v-if="infoSec.isagent">
            <div>代理身份</div>
            <div class="name">
                <div>{{infoSec.rolename}}</div>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell" v-if="infoSec.isagent">
            <div>代理身份到期时间</div>
            <div class="name">
                <div>{{infoSec.overtime}}</div>
            </div>
        </div>
        <div class="line"></div>
        <div class="cell" v-if="infoSec.owner">
            <div>群主身份到期时间</div>
            <div class="name">
                <div>{{infoSec.owner_overtime}}</div>
            </div>
        </div>
        <div class="btn-db" v-show="hidshow">
            <button-normal
                    widthPro="200"
                    heightPro="35"
                    :textBtn="textBtn"
                    color="#fff"
                    size="14"
                    @click.native="toChange"
            ></button-normal>
        </div>
        <van-popup v-model="isShow" class="per-p" position="bottom">
            <van-picker :columns="columns"
                        :show-toolbar="true"
                        @confirm="onConfirm"
                        @cancel="onCancel"
            ></van-picker>
        </van-popup>
    </div>
</template>

<script>
    import API from '../../request/api';
    import dayjs from 'dayjs';
    import buttonNormal from '../../components/common/buttonNormal';
    import regular from '@/common/regular';
    import {commonMixin} from '@/common/mixin';

    export default {
        /**
         * province,city,area
         * 个人信息修改
         * */
        components: {buttonNormal},
        mixins: [commonMixin],
        data() {
            return {
                columns: ['男', '女'],
                isShow: false,
                genderIndex: 0,
                textBtn: '确认',
                docmHeight: '0',  //默认屏幕高度
                showHeight: '0',  //实时屏幕高度
                hidshow: true, //显示或者隐藏footer,
                isResize: false, //默认屏幕高度是否已获取
                infoSec: {}
            };
        },
        computed: {
            gender() {
                if (this.userInfo.gender === '1') {
                    return '男';
                } else if (this.userInfo.gender === '2') {
                    return '女';
                } else if (this.userInfo.gender === '0') {
                    return '未设置';
                }
            },
            areaData() {
                return this.userInfo.province_name + this.userInfo.city_name + this.userInfo.area_name;
            }
        },
        watch: {
            showHeight() {
                // alert(this.docmHeight + this.showHeight);
                if (this.docmHeight < this.showHeight) {
                    this.hidshow = true;
                } else {
                    this.hidshow = false;
                }
            }
        },
        mounted() {
            // window.onresize监听页面高度的变化 
            window.onresize = () => {
                return (() => {
                    if (!this.isResize) {
                        //  //默认屏幕高度               
                        this.docmHeight = document.body.clientHeight;
                        this.isResize = true;
                    }
                    //     //实时屏幕高度                   
                    this.showHeight = document.documentElement.clientHeight;
                })();
            };
        },
        methods: {
            focus() {
                if (this.userInfo.age === '0') {
                    this.userInfo.age = '';
                }
            },
            changeAvatar(e) {
                let file = e.target.files[0];
                console.log(file, '阿萨飒飒的');
                const type = ['image/jpeg', 'image/jpg', 'image/png'];
                if (type.includes(file.type)) {
                    this.showGlobaLoading();
                    // todo 头像选择
                    this.request.upPic({type: 1, file: file}).then(res => {
                        this.userInfo.avatar = res;
                        this.hideGlobaLoading();
                    }, (res) => {
                    });
                } else {
                    this.$toast('请上传正确格式的图片(jpg/png)');
                }

            },
            toChange() {
                //nickname	String	否	昵称
                // avatar	String	否	头像地址
                // gender	Int	否	性别 1.男 2.女
                // infophone	手机号	否	15006885367
                // age	int	否	年龄 90
                let regue = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/;
                if (this.userInfo.nickname.length > 12) {
                    this.$toast('昵称长度不要超过12个汉字');
                } else if (!regue.test(this.userInfo.nickname)) {
                    this.$toast('请勿使用特殊字符');
                } else {
                    let data = {
                        nickname: this.userInfo.nickname,
                        avatar: this.userInfo.avatar,
                        gender: this.genderIndex,
                        age: this.userInfo.age,
                        infophone: this.userInfo.infophone || 0
                    };
                    this.request.post(API.infoChange, data).then(res => {
                        this.$toast('保存成功');
                        this.$router.back();
                    }, (err) => {
                        console.log(err);
                    });
                }

            },
            onCancel() {
                this.isShow = false;
            },
            onConfirm(value, index) {
                console.log(value, index, '0000');
                if (index === 0) {
                    this.userInfo.gender = '1';
                    this.genderIndex = '1';
                } else if (index === 1) {
                    this.userInfo.gender = '2';
                    this.genderIndex = '2';
                }
                this.isShow = false;
            },
            mett(e) {
                console.log(e, '11111111');
            }

        },
        beforeCreate() {
            // 可以在这加个loading事件，在加载实例时触发
            // 这个阶段实例的data、methods是读不到的
        },
        created() {
            this.request.get(API.infoSec).then((res) => {
                this.infoSec = res;
                this.infoSec.owner_overtime = dayjs(this.infoSec.owner_overtime).format('YYYY-MM-DD');
                this.infoSec.overtime = dayjs(this.infoSec.overtime).format('YYYY-MM-DD');

            });
            // 初始化数据，如在这结束loading事件，异步ajax请求也适宜在这里调用
            // 实例创建后：这个阶段已经完成了数据观测(data observer)，属性和方法的运算， watch/event 事件回调。
            // mount挂载阶段还没开始，"el" 属性目前不可见，数据并没有在DOM元素上进行渲染
        },
        beforeMount() {
            // 相关的 render 函数首次被调用。
        },
        beforeUpdate() {
            // 数据更新时调用，但不进行DOM重新渲染，在数据更新时DOM没渲染前可以在这个生命函数里进行状态处理
        },
        updated() {
            //  如果对数据统一处理，在这里写上相应函数
            // 这个状态下数据更新并且DOM重新渲染，当这个生命周期函数被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。当实例每次进行数据更新时updated都会执行
        },
        beforeDestroy() {
            // 可以做一个确认停止事件的确认框
        },
        destroyed() {
            // Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，所有的事件监听器会被移除，所有的子实例也会被销毁
        }
    };

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    @import '~assets/main.styl'
    .van-uploader {
        width 100vw
        position relative

        .file {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            cursor: pointer;
        }
    }

    .per-p {
        width 100vw
    }

    .cell {
        cell()

        .pic {
            width vw(45)
            height vw(45)
            border-radius vw(25)
        }

        .name {
            flex-x()

            input {
                text-align right
                margin-right vw(10)
            }
        }

        .right {
            arrow()
        }
    }

    .head {
        height vw(60)
        line-height vw(60)
    }

    .btn-db {
        position fixed
        width 100vw
        bottom 0
        background white
    }

    .nav-hide {
        position: static !important;
    }

    .sure {
        btn-center()
    }
</style>
